﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Threading;

namespace Common.Background.Helper
{

    /// <summary>
    /// 处理上下文信息
    /// </summary>
    public class UContext
    {
        public static UContext current
        {
            get
            {
                HttpContext hc = HttpContext.Current;
                UContext ux = null;
                if (ux == null)
                {
                    ux = GextItem(hc);
                    if (ux == null)
                    {
                        ux = new UContext();
                        SaveItem(ux);
                    }
                }
                return ux;
            }

        }
        public static HttpContext publicContext
        {
            get
            {
                return HttpContext.Current;
            }
        }

        public static bool IsNull()
        {
            if (publicContext != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static UContext GextItem(HttpContext ht)
        {
            if (ht != null)
            {
                return ht.Items["U_ContextStore"] as UContext;
            }
            else
            {
                return Thread.GetData(Thread.GetNamedDataSlot("U_ContextStore")) as UContext;
            }
        }
        public static void SaveItem(UContext uc)
        {

            if (UContext.IsNull())
            {
                publicContext.Items["U_ContextStore"] = uc;
            }
            else
            {
                Thread.SetData(Thread.GetNamedDataSlot("U_ContextStore"), uc);
            }
        }

        /// <summary>
        /// 获取程序集的名称
        /// </summary>   
        /// <returns></returns>
        public string GetAss()
        {
            Assembly aly = Assembly.GetExecutingAssembly();
            string Name = aly.GetName().Name;  //获取命名空间
            return Name;
        }
    }
}